clear			all
set 			more off
cd 				"~/Dropbox/Indonesia Migration/" // Specify your root directory here
/*******************************************************************************
Bootstrap: Native Employment
********************************************************************************/
use				"Data/Merged", clear
label 			var per_immig_cst_5Y "Population share of new migrants in year t"
label 			var per_immig_cst_5Y_lag "Population share of new migrants in year t-5"

foreach 		var in emp native_emp mig_emp_o mig_emp_n ///
				wage native_wage mig_wage_o mig_wage_n ///
				SE native_SE mig_SE_o mig_SE_n ///
				unpaid native_unpaid mig_unpaid_o mig_unpaid_n {
replace			prop_`var' = prop_`var' * 100
}	

tempfile 		bootstrap
save 			`bootstrap'

clear 
local 			boots = 10000
set 			obs `boots'
gen 			store_F1 = .
gen 			store_F2 = .
gen 			KP_F = .
gen 			CD_Wald = .
gen 			store_B1 = .
gen 			store_B2 = .
gen 			store_SE1 = .
gen 			store_SE2 = .

quietly {
forvalues 		i = 1(1)`boots' {
	if floor((`i'-1)/100) == (`i'-1)/100 {
		noisily display "Working on `i' out of `boots' at $S_TIME"
	}
	
	preserve
	use 		`bootstrap', clear
	gen 		x = runiform()
	bsample
	
ivreghdfe		prop_native_emp (per_immig_cst_5Y per_immig_cst_5Y_lag = ///
				pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) ///
				av_high_educ_ct prop_male_ct av_age_ct ///
				[pweight = x], a(i.year#i.geolev1 i.geolev2) cl(geolev2) first	
mat				c = e(first)
local			SWF1= c[8,1]
local			SWF2= c[8,2]
local 			KP_F= `e(widstat)'
local 			CD_Wald= `e(cdf)'
local			B1= _b[per_immig_cst_5Y]
local			B2= _b[per_immig_cst_5Y_lag]
local			SE1= _se[per_immig_cst_5Y]
local			SE2= _se[per_immig_cst_5Y_lag]
	
	restore 

	replace store_F1 = `SWF1' in `i'
	replace store_F2 = `SWF2' in `i'
	replace KP_F = `KP_F' in `i'
	replace CD_Wald = `CD_Wald' in `i'
	replace store_B1 = `B1' in `i'
	replace store_B2 = `B2' in `i'
	replace store_SE1 = `SE1' in `i'
	replace store_SE2 = `SE2' in `i'
}
}

save 			"Data/bootstrap_results", replace

/*******************************************************************************
Bootstrap: Native Employment
********************************************************************************/
use				"Data/Merged", clear
label 			var per_immig_cst_5Y "Population share of new migrants in year t"
label 			var per_immig_cst_5Y_lag "Population share of new migrants in year t-5"

foreach 		var in emp native_emp mig_emp_o mig_emp_n ///
				wage native_wage mig_wage_o mig_wage_n ///
				SE native_SE mig_SE_o mig_SE_n ///
				unpaid native_unpaid mig_unpaid_o mig_unpaid_n {
replace			prop_`var' = prop_`var' * 100
}	

tempfile 		bootstrap
save 			`bootstrap'

clear 
local 			boots = 10000
set 			obs `boots'
gen 			store_B1 = .
gen 			store_B2 = .
gen 			store_SE1 = .
gen 			store_SE2 = .

quietly {
forvalues 		i = 1(1)`boots' {
	if floor((`i'-1)/100) == (`i'-1)/100 {
		noisily display "Working on `i' out of `boots' at $S_TIME"
	}
	
	preserve
	use 		`bootstrap', clear
	gen 		x = runiform()
	bsample
	
reghdfe			prop_emp ///
				pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y ///
				av_high_educ_ct prop_male_ct av_age_ct ///
				[pweight = x], a(i.year#i.geolev1 i.geolev2) cl(geolev2)	
local			B1= _b[pred_per_immig_cst_10Y_5Y]
local			B2= _b[pred_per_immig_cst_Jaeger_10Y]
local			SE1= _se[pred_per_immig_cst_10Y_5Y]
local			SE2= _se[pred_per_immig_cst_Jaeger_10Y]
	
	restore 

	replace store_B1 = `B1' in `i'
	replace store_B2 = `B2' in `i'
	replace store_SE1 = `SE1' in `i'
	replace store_SE2 = `SE2' in `i'
}
}

save 			"Data/bootstrap_reduced_results", replace

/*******************************************************************************
Figures: First-stage F-Statistics
********************************************************************************/

*SW F-statistic from weighted regression is 570.05

use 			"Data/bootstrap_results", replace

preserve
keep if KP_F<350
gen F_actual = 575.05
histogram 		KP_F, percent scheme(s1color) bin(20) gap(10) bcolor(lavender) xtitle("F-statistic") ytitle("Percentage") yscale(titlegap(2)) xscale(titlegap(2)) xline(575.05) xsize(12) ysize(8) xsc(r(0 600)) xlabel(0 (100) 600)

graph 			export "Figures/bootstrap_F.jpg", as(jpg) name("Graph") quality(100) replace
restore

/*******************************************************************************
Figures: Reduced-form Coefficient
********************************************************************************/

use				"Data/Merged", clear
label 			var per_immig_cst_5Y "Population share of new migrants in year t"
label 			var per_immig_cst_5Y_lag "Population share of new migrants in year t-5"

foreach 		var in emp native_emp mig_emp_o mig_emp_n ///
				wage native_wage mig_wage_o mig_wage_n ///
				SE native_SE mig_SE_o mig_SE_n ///
				unpaid native_unpaid mig_unpaid_o mig_unpaid_n {
replace			prop_`var' = prop_`var' * 100
}	
reghdfe			prop_native_emp ///
				pred_per_immig_cst_10Y_5Y ///
				av_high_educ_ct prop_male_ct av_age_ct ///
				[pweight = pop_cst], a(i.year#i.geolev1 i.geolev2) cl(geolev2)	

*Reduced form coefficient -.2565907

use 			"Data/bootstrap_reduced_results", replace
gen 			Reduced_main = -.2565907

preserve
keep if store_B1<0
histogram 		store_B1_new, percent scheme(s1color) bin(20) gap(10) bcolor(lavender) xtitle("Reduced-form Regression Coefficient") ytitle("Percentage") yscale(titlegap(2)) xscale(titlegap(2)) xline(-.2565907) xsize(12) ysize(8) xsc(r(-1.7 0)) xlabel(-1.5 (0.5) 0)

graph 			export "Figures/bootstrap_reduced.jpg", as(jpg) name("Graph") quality(100) replace
restore

















